/*
* Copyright 2011 Pavel Kozhin 
*
* Licensed under the Apache License, Version 2.0 (the "License"); 
* you may not use this file except in compliance with the License. 
* You may obtain a copy of the License at 
* 
* http://www.apache.org/licenses/LICENSE-2.0
* 
* Unless required by applicable law or agreed to in writing, software 
* distributed  under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
* See the License for the specific language governing permissions and 
* limitations under the License.
*
* Author: Pavel Kozhin.
*/
package org.etherframework.controller
{
	import flash.events.Event;
	import flash.utils.Dictionary;
	
	import org.etherframework.IEther;
	import org.etherframework.common.command.ICommand;
	import org.etherframework.common.command.ICommands;
	import org.etherframework.core.$Globals;
	import org.etherframework.core.GlobalConfig;
	import org.etherframework.core.bridge.IBridge;
	import org.etherframework.core.bridge.IBridgeFacades;
	import org.etherframework.core.bridge.IObjectController;
	import org.etherframework.core.context.Context;
	import org.etherframework.core.context.IContexts;
	import org.etherframework.core.context.IContextual;
	import org.etherframework.core.context.ISingleContextual;
	import org.etherframework.core.module.IModulePartFacade;
	import org.etherframework.core.module.ModulePart;
	import org.etherframework.core.module.ModulePartFactory;
	import org.etherframework.core.utils.ClassUtils;

	public class Commands extends ModulePart implements ICommands, ISingleContextual
	{
		private static var _config:GlobalConfig;
		
		private var _context:Context;
		private var _owner:ICommands;
		private var _rules:Dictionary = new Dictionary();
		private var _facade:IBridgeFacades;
		private var _contexts:IContexts;
		private var _localEther:IEther;
		
		public static function initialize():void
		{
			if ($Globals.config != _config)
			{
				$Globals.config.addObjectProcessor(new Processor());
				$Globals.config.commandsFactory = new ModulePartFactory(Commands);
				
				_config = $Globals.config;
			}
		}
		
		public function Commands(owner:ICommands, context:Context)
		{
			if (!context)
				throw new ArgumentError("Argument context:Context can not be null.");
			
			_owner = owner;
			_context = context;
		}
		
		override public function init(bridge:IBridge):void
		{
			super.init(bridge);
			
			_facade = bridge.facade;
			_localEther = bridge.createEther(_context);
		}
		
		public function hasMapping(event:String):Vector.<Class>
		{
			return (_rules[event] ? _rules[event].concat() : null) || 
				(_owner ? _owner.hasMapping(event) : null);
		}
		
		public function map(event:String, command:Class):void
		{
			if (!event)
				throw new ArgumentError("Argument type:String can not be empty.");
			if (!command)
				throw new ArgumentError("Argument command:Class can not be null.");
			if (_rules[event] && _rules[event].indexOf(command) != -1)
				throw new Error("Event '"+event+"' is already assigned to command " + command + ".");
			if (!ClassUtils.implementsInterface(command, ICommand))
				throw new Error("Class " + command + " must implement " +
					"org.etherframework.common::ICommand interface.");
			
			if (!_rules[event])
			{
				bridge.interaction.listenEvent(_localEther, event, eventHandler);
			}
			
			const commands:Vector.<Class> = _rules[event] || 
				(_rules[event] = new Vector.<Class>());
			
			commands.push(command);
		}
		
		public function unmap(event:String, command:Class):void
		{
			if (!event)
				throw new ArgumentError("Argument type:String can not be empty.");
			if (!command)
				throw new ArgumentError("Argument command:Class can not be null.");
			if (!_rules[event] || _rules[event].indexOf(command) == -1)
				throw new Error("Event '"+event+"' is not assigned to the command " + command + ".");
			
			const commands:Vector.<Class> = _rules[event];
			
			commands.splice(commands.indexOf(command), 1);
			
			if (!commands.length)
			{
				bridge.interaction.unlistenEvent(_localEther, event, eventHandler);
			}
		}
		
		override public function destroy():void
		{
			_contexts.dispose(this);
		}
		
		public function contextual(context:Context):ICommands
		{
			return _contexts.contextual(context, ICommands, this) as ICommands;
		}
		
		public function create(context:Context, type:Class):IContextual
		{
			const result:Commands = new Commands(this, context);
			result.init(bridge);
			return result;
		}
		
		public function dispose():void
		{
			_rules = null;
			_facade = null
			bridge.interaction.unlistenAll(_localEther);
			bridge = null;
		}
		
		override public function getFacade(owner:IBridgeFacades):IModulePartFacade
		{
			return new CommandsFacade(owner, this);
		}
		
		public function get context():Context { return _context; }
		
		private function eventHandler(event:Event):void
		{
			var command:Object;
			var processed:Object;
			const commands:Vector.<Class> = _rules[event.type] as Vector.<Class>;
			
			for each (var item:Class in commands)
			{
				processed = bridge.process(item, null, null, new GesturesHelper(event));
				
				command = (processed is IObjectController) ? 
					IObjectController(processed).target : 
					processed; 
				
				if (command is IGestureCommand)
					IGestureCommand(command).event = event;
				
				if (!command)
					throw new Error("Dependency for " + item + " could not be found. " +
						"Please, make sure that it was added to dependencies.");
				
				command.execute();
			}
		}
	}
}

import flash.events.Event;

import org.etherframework.core.common.IArgumentFactory;
import org.etherframework.core.context.Context;
import org.etherframework.core.reflection.Parameter;

final class GesturesHelper implements IArgumentFactory
{
	private var _event:Event;
	
	public function GesturesHelper(event:Event)
	{
		_event = event;
	}
	
	public function createArgument(parameter:Parameter, context:Context):Object
	{
		return (_event["constructor"] == parameter.type) ? _event : null;
	}
}